package com.goke.media.call;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.Log;

import com.goke.media.jni.SDPInfo;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

public class PeerInfo extends SDPInfo {

    PeerInfo(int port, String addr, String codec){
        super(port, addr, codec);
    } 

    public static String getLocalIP(Context context){
        try {
            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
                NetworkInterface intf = en.nextElement();
                for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();)
                {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress() && (inetAddress instanceof Inet4Address))
                    {
                        return inetAddress.getHostAddress().toString();
                    }
                }
            }
        }
        catch (SocketException ex){
            ex.printStackTrace();
        }
        return null;
    }

    public static String getThreadId() {
        try {
            long id = Thread.currentThread().getId();
            return String.valueOf(id);
        } catch (Exception e) {
            return null;
        }
    }
    
 // Checks for and communicate failures to user (logcat and popup).
 	  public static void check(Context context, boolean value, String message) {
 	    if (value) {
 	      return;
 	    }
 	    Log.e("cosdk-check", message);
 	    AlertDialog alertDialog = new AlertDialog.Builder(context).create();
 	    alertDialog.setTitle("WebRTC Error");
 	    alertDialog.setMessage(message);
 	    alertDialog.setButton(DialogInterface.BUTTON_POSITIVE,
 	        "OK",
 	        new DialogInterface.OnClickListener() {
 	          public void onClick(DialogInterface dialog, int which) {
 	            dialog.dismiss();
 	            return;
 	          }
 	        }
 	                          );
 	    alertDialog.show();
 	  }
}
